﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using WebApp.Commons;
using WebApp.Data;

namespace WebApp.Services
{
    public class EmployeeService
    {
        public List<tbl_user> GetEmployees(ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                count = db.tbl_users.Count();

                return db.tbl_users.ToList();
            }
        }
        
        public List<tbl_user> GetEmployees(int index, int size,
                                           string sortColumn, string sortOrder, string keyword,ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                var direction = sortOrder.Equals("desc") ? " descending " : "";
                count = db.tbl_users.Count(u => u.user_login_name.Contains(keyword));

                //return db.tbl_users.Where(u => u.user_login_name.Contains(keyword)).OrderBy(sortColumn + direction).ToList();
                return db.tbl_users.Where(u => u.user_login_name.Contains(keyword)).ToList();
            
            }
        }

        public List<tbl_user_type> GetEmployeeTypes()
        {
            using (var db = new PAVINGDataContext())
            {
                return db.tbl_user_types.ToList();
            }
        }

        public bool UpdateEmployee(tbl_user model)
        {
            try
            {
                using (var db = new PAVINGDataContext())
                {
                    if (model.user_id == 0)
                    {
                        model.user_create_by = PavingContext.CurrentUserId();
                        model.user_create_date = DateTime.Now;

                        string newUserDigit = (DataProvider.GetLastUserId() +1).ToString();
                        int addZero = 4 - model.user_id.ToString().Length;
                        for (int i = 0; i < addZero; i++)
                        {
                            newUserDigit = string.Format("0{0}", newUserDigit);
                        }
                        string typeCode = DataProvider.GetEmployeeTypeCode(model.user_type_id);
                        model.user_login_name = string.Concat(typeCode, newUserDigit);

                        //NOTE:Regex (?!.{5}).*  => To get the last 4 characters
                        model.user_password = Regex.Match(model.user_id_no, "(?!.{5}).*").Value ;
                        db.tbl_users.InsertOnSubmit(model);
                    }
                    else
                    {
                        tbl_user userModel = db.tbl_users.SingleOrDefault(s => s.user_id.Equals(model.user_id));
                        if(userModel != null)
                        {
                            userModel.user_type_id = model.user_type_id;
                            userModel.user_firstname = model.user_firstname;
                            userModel.user_lastname = model.user_lastname;
                            if (!string.IsNullOrEmpty(model.user_id_no))
                                userModel.user_id_no = model.user_id_no;
                            userModel.user_tel_no = model.user_tel_no;
                            userModel.user_status = model.user_status;
                            if(!string.IsNullOrEmpty(model.user_password))
                                userModel.user_password = model.user_password;

                            userModel.user_update_by = PavingContext.CurrentUserId();
                            userModel.user_update_date = DateTime.Now;
                        }
                    }
                    db.SubmitChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("UpdateEmployee : " + ex.Message);
                return false;
            }
        }

        public bool DeleteEmployee(tbl_user model)
        {
            try
            {
                using (var db = new PAVINGDataContext())
                {
                    var selectedModel = db.tbl_users.SingleOrDefault(s => s.user_id.Equals(model.user_id));

                    if (selectedModel != null)
                    {
                        db.tbl_users.DeleteOnSubmit(selectedModel);
                        db.SubmitChanges();

                        return true;
                    }

                    return false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("DeleteEmployee : " + ex.Message);
                return false;
            }
        }

        public bool ChangePassword(tbl_user model)
        {
            try
            {
                using (var db = new PAVINGDataContext())
                {
                    tbl_user userModel = db.tbl_users.SingleOrDefault(s => s.user_id.Equals(model.user_id));
                    if (userModel != null)
                    {
                        if (!string.IsNullOrEmpty(model.user_password))
                            userModel.user_password = model.user_password;

                        userModel.user_update_by = PavingContext.CurrentUserId();
                        userModel.user_update_date = DateTime.Now;
                        db.SubmitChanges();
                    }
                    return true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("UpdateEmployee : " + ex.Message);
                return false;
            }
        }

    }
}